﻿using STT.Business;
using STT.Layer.Connection;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace STT
{
    public partial class Default : System.Web.UI.Page
    {
        ToolBox tb = new ToolBox();
        Control ct = new Control();
        BTbDangNhap dn = new BTbDangNhap();
        Data db = new Data();
        string module = "";
        string key = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.Cookies["UserinfoDN"] != null)
                {
                    LoadSessionbycookie();
                    lbltendn.Text = Session["TenDN"].ToString();
                    lbltendn.Visible = true;
                    hello.Visible = true;
                    lg.Visible = false;
                    if (Session["rolehvgv"].ToString() == "4")
                        lbltendn.NavigateUrl = "~/Profile/Default.aspx";
                    else if (Session["rolehvgv"].ToString() == "5")
                        lbltendn.NavigateUrl = "~/Profile/Default.aspx";
                }
                else if (Session["TenDN"] != null && Session["rolehvgv"] != null)
                {
                    lbltendn.Text = Session["TenDN"].ToString();
                    lbltendn.Visible = true;
                    hello.Visible = true;
                    lg.Visible = false;
                    if (Session["rolehvgv"].ToString() == "4")
                        lbltendn.NavigateUrl = "~/Profile/Default.aspx";
                    else if (Session["rolehvgv"].ToString() == "5")
                        lbltendn.NavigateUrl = "~/Profile/Default.aspx";
                }

                HtmlGenericControl gel = new HtmlGenericControl("div");
                if (Request.QueryString["mod"] == null)
                {
                    Response.Redirect("Default.aspx?mod=Home");
                }
                else
                {
                    module = Request.QueryString["mod"].ToString().Trim();

                    if (module == "Home")
                    {

                        ct = Page.LoadControl("~/Home/UcIndex.ascx");

                    }
                    if (module == "LHQT1314")
                    {

                        ct = Page.LoadControl("~/Home/UcLienHe.ascx");


                    }
                    if (module == "KHNH1411")
                    {

                        ct = Page.LoadControl("~/Home/UcKhoaHoc.ascx");


                    }
                    if (module == "GT1303")
                    {

                        ct = Page.LoadControl("~/Home/UcGioiThieu.ascx");

                    }
                    if (module == "TK1303")
                    {

                        ct = Page.LoadControl("~/Home/UcTimKiem.ascx");

                    }  
                    else if (module == "TTSK1303")
                    {
                        key = Request.QueryString["key"].ToString().Trim();
                        SqlDataReader dr;
                        SqlConnection cn = new SqlConnection(db.str);
                        db.OpenConnection(cn);

                        SqlCommand cmd1 = new SqlCommand("[GetId_TinTuc]", cn);
                        cmd1.CommandType = CommandType.StoredProcedure;

                        dr = cmd1.ExecuteReader();

                        while (dr.Read())
                        {
                            if (key == dr[0].ToString().Trim())
                            {
                                db.ct = Page.LoadControl("~/Home/UcTinTucSuKien.ascx");
                            }
                        }
                        dr.Close();
                        ct = Page.LoadControl("~/Home/UcTinTucSuKien.ascx");

                    }

                }
                gel.Controls.Add(ct); plhome.Controls.Add(gel);
                LoadTT();
            }
            catch{}
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        private void LoadTT()
        {
            DataTable dt = tb.GetDataTableById2("TbTinTuc_GetByLoaiTT","1");
            cpl2.PageSize = 3;
            cpl2.DataSource = dt.DefaultView;
            cpl2.BindToControl = dlltintuc;
            dlltintuc.DataSource = cpl2.DataSourcePaged;
            dlltintuc.DataBind();
           
        }
        protected void lbtthoat_Click(object sender, EventArgs e)
        {
            Session["Magv"] = null;
            Session["rolehvgv"] = null;
            Session["Mahv"] = null;
            Session["TenDN"] = null;
            Session["DonVi"] = null;
            Response.Cookies["UserinfoDN"].Expires = DateTime.Now.AddDays(-1);
            Response.Redirect("~/Default.aspx");
        }
        protected void btnSignin_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection cn = new SqlConnection(db.str))
                {
                    db.OpenConnection(cn);
                    DataTable dt = db.GetDataTableById2("[GetTkDangNhapByTenDN]", username.Text);
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["Khoataikhoan"].ToString() == "0")
                        {
                            if (dt.Rows[0]["MatKhau"].ToString().Trim() == dn.GetMD5(password.Text.Trim() + dt.Rows[0]["RadomPass"].ToString().Trim()))
                            {
                                if (dt.Rows[0]["Quyen"].ToString().Trim() == "4")
                                {
                                    DataTable hv = db.GetDataTableById2("[Get_HocVien_ByMaHV]",username.Text);
                                    if (ckghinho.Checked == true)
                                    {
                                        Response.Cookies["UserinfoDN"]["Username"] = username.Text;
                                        Response.Cookies["UserinfoDN"]["pass"] = password.Text;
                                        Response.Cookies["UserinfoDN"].Expires = DateTime.Now.AddDays(15);
                                    }
                                    else
                                    {
                                        Response.Cookies["UserinfoDN"]["Username"] = username.Text;
                                        Response.Cookies["UserinfoDN"]["pass"] = password.Text;
                                        Response.Cookies["UserinfoDN"].Expires = DateTime.Now.AddDays(0.001);
                                    }
                                    //Session["rolehvgv"] = "4";
                                    //Session["Mahv"] = username.Text;
                                    //Session["TenDN"] = username.Text;
                                    //Session["Anh"] = hv.Rows[0]["Anh"].ToString();
                                    //Session["Chucvu"] = hv.Rows[0]["TenChucVu"].ToString();
                                    //Session["TenNguoiDung"] = hv.Rows[0]["TenHV"].ToString() +" "+ hv.Rows[0]["Ten"].ToString();
                                    Response.Redirect("~/Profile/Default.aspx");
                                    
                                }
                                else if (dt.Rows[0]["Quyen"].ToString().Trim() == "5")
                                {
                                    DataTable gv = db.GetDataTableById2("[GiangVien_GetByMagv]", username.Text);
                                    if (ckghinho.Checked == true)
                                    {
                                        Response.Cookies["UserinfoDN"]["Username"] = username.Text;
                                        Response.Cookies["UserinfoDN"]["pass"] = password.Text;
                                        Response.Cookies["UserinfoDN"].Expires = DateTime.Now.AddDays(15);

                                    }
                                    else
                                    {
                                        Response.Cookies["UserinfoDN"]["Username"] = username.Text;
                                        Response.Cookies["UserinfoDN"]["pass"] = password.Text;
                                        Response.Cookies["UserinfoDN"].Expires = DateTime.Now.AddDays(0.001);
                                    }
                                    //Session["rolehvgv"] = "5";
                                    //Session["Magv"] = username.Text;
                                    //Session["TenDN"] = username.Text;
                                    //Session["Anh"] = gv.Rows[0]["Anh"].ToString();
                                    //Session["TenNguoiDung"] = gv.Rows[0]["TenGV"].ToString();
                                    Response.Redirect("~/Profile/Default.aspx");
                                }
                                else
                                {
                                    lblthongbao.Visible = true;
                                    ThongBao("Tài khoản hoặc mật khẩu sai....!");
                                }

                            }
                            else
                            {
                                ThongBao("Tài khoản hoặc mật khẩu sai....!");
                                lblthongbao.Visible = true;


                            }
                        }

                        else
                        {
                            ThongBao("Tài khoản hoặc mật khẩu sai....!");
                            lblthongbao.Visible = true;

                        }
                    }
                    else
                    {
                        ThongBao("Tài khoản hoặc mật khẩu sai....!");
                        lblthongbao.Visible = true;

                    }
                }
            }
            catch { }
        }
        public void LoadSessionbycookie()
        {
            try
            {
                DataTable dt = db.GetDataTableById2("[GetTkDangNhapByTenDN]", Request.Cookies["UserinfoDN"]["Username"]);
                if (dt.Rows[0]["Quyen"].ToString().Trim() == "4")
                {
                    DataTable hv = db.GetDataTableById2("[Get_HocVien_ByMaHV]", Request.Cookies["UserinfoDN"]["Username"]);
                    Session["rolehvgv"] = "4";
                    Session["Mahv"] = Request.Cookies["UserinfoDN"]["Username"];
                    Session["TenDN"] = Request.Cookies["UserinfoDN"]["Username"];
                    Session["TenNguoiDung"] = (hv.Rows[0]["TenHV"].ToString() + " " + hv.Rows[0]["Ten"].ToString());
                    Session["Anh"] = hv.Rows[0]["Anh"].ToString();
                    Session["DonVi"] = hv.Rows[0]["MaDonvi"].ToString();
                    Session["Chucvu"] = hv.Rows[0]["TenChucVu"].ToString();
                    //Response.Redirect("~/Profile/Default.aspx");
                }
                else if (dt.Rows[0]["Quyen"].ToString().Trim() == "5")
                {
                    DataTable gv = db.GetDataTableById2("[GiangVien_GetByMagv]", Request.Cookies["UserinfoDN"]["Username"]);
                    Session["rolehvgv"] = "5";
                    Session["Magv"] = Request.Cookies["UserinfoDN"]["Username"];
                    Session["TenDN"] = Request.Cookies["UserinfoDN"]["Username"];
                   
                    Session["TenNguoiDung"] = gv.Rows[0]["TenGV"].ToString(); 
                    Session["Anh"] = gv.Rows[0]["Anh"].ToString();
                    //Session["Magv"] = username.Text;
                    //Response.Redirect("~/Profile/Default.aspx");
                }
            }
            catch { }
        }

        protected void btntimkiem_Click(object sender, EventArgs e)
        {
            try {
                string search = txttimkiem.Text.Trim();
                Response.Redirect("Default.aspx?mod=TK1303&key="+search);
            }
            catch { }
            
        }
        protected void btntimkiem2_Click()
        {
            try
            {
                string search = txttimkiem.Text.Trim();
                Response.Redirect("Default.aspx?mod=TK1303&key=" + search);
            }
            catch { }
        }
    }
}